package com.mdt.doforms.bienen.util;

import android.util.Log;

public class HtdLog {
	private static final String LOGTAG = "HtdLog";
	public enum LogLevel {
		LOGLEVEL_None,
		LOGLEVEL_Error,
		LOGLEVEL_Warning,
		LOGLEVEL_Info,
		LOGLEVEL_Debug,
		LOGLEVEL_Verbose
	}

	
	private static LogLevel m_nLogLevel = LogLevel.LOGLEVEL_Debug;
   
	/**
	 * Sets the log level as a value from LogLevel enum.
	 * Default value for the log level is LOGLEVEL_Warning.
	 * Besides this method, log level is set by VbConfSet TraceLevel property.
	 * @param level the logging level
	 * @see VbConfSet#setTraceLevel(String)
	 */
	public static void setLogLevel(LogLevel level) {
		m_nLogLevel = level;
	}

	static void setLogLevel(String level) {
		if(level.equalsIgnoreCase("NONE")) m_nLogLevel = LogLevel.LOGLEVEL_None;
		else if(level.equalsIgnoreCase("ERROR")) m_nLogLevel = LogLevel.LOGLEVEL_Error;
		else if(level.equalsIgnoreCase("WARNING")) m_nLogLevel = LogLevel.LOGLEVEL_Warning;
		else if(level.equalsIgnoreCase("INFO")) m_nLogLevel = LogLevel.LOGLEVEL_Info;
		else if(level.equalsIgnoreCase("DEBUG")) m_nLogLevel = LogLevel.LOGLEVEL_Debug;
		else if(level.equalsIgnoreCase("VERBOSE")) m_nLogLevel = LogLevel.LOGLEVEL_Verbose;
		else m_nLogLevel = LogLevel.LOGLEVEL_Warning;
	}

	/**
	 * Stores a string in the system logcat stream if the class LogLevel is LOGLEVEL_Debug or higher.
	 * @param logTag the message tag: typically, the capitalized class name saved in a class static variable. 
	 * @param szText the message text.
	 */
	public static void d(String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, LOGTAG, szText);
	}
	
	public static void i( String szText){
		WriteLog(LogLevel.LOGLEVEL_Info, LOGTAG, szText);
	}
	
	public static void w( String szText){
		WriteLog(LogLevel.LOGLEVEL_Warning, LOGTAG, szText);
	}
	public static void v( String szText){
		WriteLog(LogLevel.LOGLEVEL_Verbose, LOGTAG, szText);
	}
	
	public static void e( String szText){
		WriteLog(LogLevel.LOGLEVEL_Error, LOGTAG, szText);
	}
	
	public static void enter( String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, LOGTAG, szText + " - IN");
	}
	
	public static void exit( String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, LOGTAG, szText + " - OUT");
	}
	
	/**
	 * Stores a string in the system logcat stream if the class LogLevel is LOGLEVEL_Debug or higher.
	 * @param logTag the message tag: typically, the capitalized class name saved in a class static variable. 
	 * @param szText the message text.
	 */
	public static void d(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, logTag, szText);
	}
	
	public static void i(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Info, logTag, szText);
	}
	
	public static void w(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Warning, logTag, szText);
	}
	public static void v(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Verbose, logTag, szText);
	}
	
	public static void e(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Error, logTag, szText);
	}
	
	public static void enter(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, logTag, szText + " - IN");
	}
	
	public static void exit(String logTag, String szText){
		WriteLog(LogLevel.LOGLEVEL_Debug, logTag, szText + " - OUT");
	}
	
	/*
	 * method used to write something that can be captured with logcat
	 */
	private static void WriteLog(LogLevel nLevel, String logTag, String szText)
	{
		if (m_nLogLevel != LogLevel.LOGLEVEL_None)
		{
			switch (nLevel)
			{
			case LOGLEVEL_Debug: Log.d(logTag, szText); break;
			case LOGLEVEL_Info: Log.i(logTag, szText); break;
			case LOGLEVEL_Warning: Log.w(logTag, szText); break;
			case LOGLEVEL_Error: Log.e(logTag, szText); break;
			case LOGLEVEL_Verbose: Log.v(logTag, szText); break;
			}
		}		
	}

	/*
	 * method used to write useful info that can be captured with logcat when an exception is raised
	 */
	public static void writeException(Exception oException) {
		WriteLog(LogLevel.LOGLEVEL_Error, "Exception", "An exception (" + oException.getClass().getName() + ") has been thrown.");
		if(oException.getMessage()!= null){
			WriteLog(LogLevel.LOGLEVEL_Error,"Exception", oException.getMessage());
		}
		StackTraceElement[] aoStackTrace = oException.getStackTrace();
		for (int nIdx = 0; nIdx < aoStackTrace.length; nIdx++) {
			WriteLog(LogLevel.LOGLEVEL_Debug, "Exception", aoStackTrace[nIdx].toString());
		}
		WriteLog(LogLevel.LOGLEVEL_Error, "Exception","*************** "+oException.getClass().getName()+" ***************");
	}
	
}
